%%% Copyright 2010 Google Inc.
%%%
%%% Licensed under the Apache License, Version 2.0 (the "License");
%%% you may not use this file except in compliance with the License.
%%% You may obtain a copy of the License at
%%%
%%%     http://www.apache.org/licenses/LICENSE-2.0
%%%
%%% Unless required by applicable law or agreed to in writing, software
%%% distributed under the License is distributed on an "AS IS" BASIS,
%%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
%%% See the License for the specific language governing permissions and
%%% limitations under the License.

%%% @copyright 2010 Google Inc. All Rights Reserved.
%%% @author <jacoby@google.com> Jacoby Thwaites [http://sites/madcloud]
%%% @version {@version}
%%% @doc
%%% Tests for prog_srv module, using mock prog implementation.
%%%
%%% To run tests, start up a test node in the /ebin directory
%%% holding the .beam files. In the example below, we're giving the node
%%% the shortname `test'.
%%%
%%% `erl -name test'
%%%
%%% Then run the tests from the shell:
%%%
%%% `eunit:test(prog_srv)'
%%%
%%% @headerfile "../include/txm.hrl"

-module(prog_srv_tests).

-include_lib("eunit/include/eunit.hrl").
-include("txm.hrl").

-define(RANDOM_OP_REF, {op, {0, 0, 0}}).
-define(RANDOM_FOLDER_REF, {folder, {0, 0, 0}}).

srv_test_() ->
  % This tuple specifies a setup fun(), teardown fun() and list of test fun().
  {setup,
   % Setup starts the server with mock prog implementation.
   fun() ->
        prog_srv:start_link(prog_mock)
   end,

   % Cleanup stops server.
   fun(_) -> prog_srv:stop()
   end,

   % List of tests, one for each server callback.
   [
     ?_assertMatch(?OK_RETURN(?ROOT_FOLDER_REF),
                   prog_srv:get_root_ref()),

     ?_assertMatch(?OK_RETURN(?REF_PTN),
                   prog_srv:get_root_ref(?RANDOM_OP_REF)),

     ?_assertMatch(?OK_RETURN(?REF_PTN),
                   prog_srv:resolve_absolute("/mock")),

     ?_assertMatch(?OK_RETURN(?REF_PTN),
                   prog_srv:resolve_relative(?RANDOM_FOLDER_REF, "backend")),

     ?_assertMatch(?OK_RETURN(?REF_PTN),
                   prog_srv:resolve_scoped(?RANDOM_FOLDER_REF, "mock")),

     ?_assertMatch(?OK_RETURN(_SomeRecord),
                   prog_srv:get_record(?RANDOM_OP_REF)),

     ?_assertMatch(?OK_RETURN(#?FIELD{}),
                   prog_srv:new_record(?RANDOM_FOLDER_REF, "dummyField",
                                      #?FIELD{})),

     ?_assertMatch(?OK_RETURN(#?FOLDER{}),
                   prog_srv:new_record(?RANDOM_FOLDER_REF, "dummyFolder",
                                       #?FOLDER{})),

     ?_assertMatch(?OK_RETURN(#?OP{}),
                   prog_srv:new_record(?RANDOM_FOLDER_REF, "dummyOp",
                                       #?OP{})),

     ?_assertMatch(?REF_PTN, prog_srv:record_to_ref(#?FOLDER{id={1, 2, 3}})),

     ?_assertMatch(?OK_RETURN(#?OP{}),
                   prog_srv:snapshot(?RANDOM_OP_REF)),

     ?_assertMatch(?OK_RETURN(#?OP{}),
                   prog_srv:snapshot(?RANDOM_OP_REF, "Some Label"))
     ]}.
